/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：
* CreateTime：2020-06-16 11:09:18
* Version：1.0
* Modify Recorder：
*************************************************/

using System.Collections.Generic;
using UnityEngine;

namespace TinyToolKit
{
    public class ShowPath : MonoBehaviour
    {
        public Transform[] pathPoints;
        public Color color = Color.green;
        [Range(0,1)] public float radius = 0.5f;
        public bool isClosed = true;

        private void OnDrawGizmos()
        {
            if (pathPoints == null || pathPoints.Length < 2) return;
            
            var tempColor = Gizmos.color;
            Gizmos.color = color;
            for (int i = 0; i < pathPoints.Length; i++)
            {
                if (radius > 0.01f)
                    Gizmos.DrawSphere(pathPoints[i].position, radius);
                if(i == pathPoints.Length - 1 && isClosed)
                    Gizmos.DrawLine(pathPoints[i].position, pathPoints[0].position);
                else if(i < pathPoints.Length - 1)
                    Gizmos.DrawLine(pathPoints[i].position, pathPoints[i + 1].position);
            }  
            Gizmos.color = tempColor;
        }
        
        [ContextMenu("AddChildren")]
        public void AddChildren()
        {
            List<Transform> temp = null;
            if (pathPoints != null && pathPoints.Length > 0)
                temp = new List<Transform>(pathPoints);
            else
                temp = new List<Transform>();

            for (int i = 0; i < transform.childCount; i++)
            {
                temp.Add(transform.GetChild(i));
            }

            pathPoints = temp.ToArray();
        }
    }
}
